/**
 * Created by kevin on 2017/6/15.
 */
/**
 * Created by kevin on 2017/6/7.
 */
var model= require('./model');
require('../utils/date');

var Packages = model.createModel({
    build : Number,
    version : String,
    robots : [{type:model.objId,ref:'Robots'}],
    branch : String,
    user : {type :model.objId,ref:'Users'},
    ctime : {type : Date,default:Date.now},
    type : Number, // 1 ios 2 android
    delegater : {type:model.objId,ref:'Delegaters'},
    app : {type:model.objId,ref:'Applications'},
    dlurl : String,
    status : {type : Number,default : 0} // 0 进行中 -1 失败 1 成功
},'Packages');

function listCallBack(err,list,callback) {
    if (err) {
        if (callback) {
            callback(err,list);
        }
    } else  {
        var cList = list.map(function (package) {
            package.createTime = package.ctime.dynamicFotmat();
            return package;
        });
        if (callback) {
            callback(err,cList);
        }
    }
}

function singleCallBack(err,doc,callback) {
    if (err) {
        if (callback) {
            callback(err,doc);
        }
    } else  {
        if (doc && doc.ctime) {
            doc.createTime = doc.ctime.dynamicFotmat();
        }
        if (callback) {
            callback(err,doc);
        }
    }
}

Packages.cleanArchiving = function () {
    Packages.update({status:0},{$set:{status:-1}},{multi: true},function (err,data) {
        console.log("已清理打包中记录");
    });
};

Packages.getCountByAppId = function (id,cb) {
    Packages.count({app:id},cb);
};

Packages.getListByAppId = function(id,callback) {

    Packages.find({app:id},null,{ lean: true ,sort:{ctime:-1}}).populate('robots').populate('user',{_id:0,passwd:0}).populate('delegater').populate('app').exec(function (err,list) {
        listCallBack(err,list,callback);
    });
};

// Packages.getByBuild = function(build,callback) {
//
//     Packages.find({build:{$regex : new RegExp(build,'i')}},null,{ lean: true ,sort:{ctime:-1}}).populate('robots').populate('user',{_id:0,passwd:0}).populate('delegater').populate('app').exec(function (err,list) {
//         listCallBack(err,list,callback);
//     });
// };

Packages.getByAppAndBuild = function(id,build,callback) {

    Packages.find({build:build,app:id},null,{ lean: true ,sort:{ctime:-1}}).populate('robots').populate('user',{_id:0,passwd:0}).populate('delegater').populate('app').exec(function (err,list) {
        listCallBack(err,list,callback);
    });
};

Packages.getPartByAppId = function(id,start,limit,callback) {

    Packages.find({app:id},null,{ skip:start,limit:limit,lean: true ,sort:{ctime:-1}}).populate('robots').populate('user',{_id:0,passwd:0}).populate('delegater').populate('app').exec(function (err,list) {
        listCallBack(err,list,callback);
    });
};

Packages.getAll = function(callback) {
    Packages.find({},null,{ lean: true ,sort:{ctime:-1}}).populate('robots').populate('user',{_id:0,passwd:0}).populate('delegater').populate('app').exec(function (err,list) {
        listCallBack(err,list,callback);
    });
};

Packages.getById = function (id,callback) {
    Packages.findById(id,null,{ lean: true ,sort:{ctime:-1}}).populate('robots').populate('user',{_id:0,passwd:0}).populate('delegater').populate('app').exec(function (err,com) {
        singleCallBack(err,com,callback);
    });
};

Packages.getByIds = function (ids,callback) {
    Packages.find({_id:{$in:ids}},null,{ lean: true ,sort:{ctime:-1}}).populate('robots').populate('user',{_id:0,passwd:0}).populate('delegater').populate('app').exec(function (err,packs) {
        listCallBack(err,packs,callback);
    });
};

Packages.edit = function (id,doc,callback) {
    Packages.findByIdAndUpdate(id, doc, callback);
};

module.exports= Packages;
